//
// Created by antler on 2018/12/6.
//

#include "Color3F.h"

#include "GlobalMacros.h"

#include "luaext/color3b/Color3B.h"
#include "luaext/utils/StringUtils.h"

Color3F *Color3F::create() {
    return new(std::nothrow)Color3F();
}

Color3F *Color3F::createWith(int rgb) {
    return new(std::nothrow)Color3F(rgb);
}

Color3F *Color3F::createWith(float r, float g, float b) {
    return new(std::nothrow)Color3F(r, g, b);
}

Color3F *Color3F::createWith(const std::string &rgb) {
    return createWith(StringUtils::stoi(rgb, 0));
}

Color3F *Color3F::createWith(const Color3B &c3b) {
    return new(std::nothrow)Color3F(c3b);
}

Color3F *Color3F::createWith(const Color3F &c3f) {
    return new(std::nothrow)Color3F(c3f);
}

Color3F::Color3F() {
    r = g = b = 0.0f;
}

Color3F::Color3F(int rgb) {
    r = ARGB_R(rgb) / 255.0f;
    g = ARGB_G(rgb) / 255.0f;
    b = ARGB_B(rgb) / 255.0f;
}

Color3F::Color3F(float r, float g, float b) {
    this->r = r;
    this->g = g;
    this->b = b;
}

Color3F::Color3F(const Color3B &c3b) {
    int rgb = c3b.toInt();
    r = ARGB_R(rgb) / 255.0f;
    g = ARGB_G(rgb) / 255.0f;
    b = ARGB_B(rgb) / 255.0f;
}

int Color3F::toInt() const {
    int rr = r * 255 + 0.5;
    int gg = g * 255 + 0.5;
    int bb = b * 255 + 0.5;

    int value = SetRGB(rr, gg, bb);
    return value;
}

std::string Color3F::toString() const {
    char buf[32];
    sprintf(buf, "0x%06x", toInt());
    return buf;
}

